(function($) {
	var $this = null;

	$(function() {
		$("#shopListPanel_close").bind("click", closeShopListPanel);
		// 关闭
		$("#shopListPanel_search").bind("click", search);
		// 查询
	});
	/**
	 * 显示商铺列表层
	 */
	function openShopListPanel(self) {
		$this = $(self);
		$this.blur();
		$("#shopListPanel, #yinying1").show();
		addMargin($("#shopListPanel"));
		$("#shopListPanel_searchName").val('');
		search();
	}

	/**
	 * 加载数据
	 */
	function search() {
		var name = $("#shopListPanel_searchName").val();
		new Pagination({
			method : 'post', // 请求方法 post|get
			type : 'json', // 返回内容格式，json|text //NOT SUPPORT
			url : '../shop/find/shopList/', // 请求地址，注意函数会自定添加分页的两个参数 ‘0/10’
			data : {
				name : name
			}, // 使用post 方法是的请求参数
			pageSize : 3, // 页面大小
			startIndex : 0, // 服务器提供的接口页码是从几开始的，建议从0开始
			success : printDataToPage, // state 为 0 时，回调
			pagerHandler : 'shopPager' // 显示分页的 div 的 id
		});
	}

	function printDataToPage(message) {
		var $tbody = $("#shopListPanel_data");
		$tbody.empty();
		// 清空数据
		var len = message.result.data.length;
		// 返回条目数
		for (var i = 0; i < len; i++) {
			// "id", "name","mp","contact");
			var id = getHtmlString(getHeadIndexValue(i, message, 'id'));
			var name = getHtmlString(getHeadIndexValue(i, message, 'name'));
			var mp = getHtmlString(getHeadIndexValue(i, message, 'mp'));

			var select = "<td><input class=\"common_btn\" type=\"button\" onclick=\"__shopSelected('" + id + "','" + name + "')\" value=\"选择\"></input></td>";

			var tr = "<tr>" + "<td>" + id + "</td>" + "<td>" + name + "</td>" + "<td>" + mp + "</td>" + select + "</tr>";
			$tbody.append($(tr));
		}
	}

	/**
	 *关闭
	 */
	function closeShopListPanel() {
		$("#shopListPanel, #yinying1").hide();
	}

	/**
	 * 选择并关闭
	 */
	function __shopSelected(shopId, shopName) {
		$this.empty();
		$this.append("<option value='" + shopId + "' selected=\"selected\" >" + shopName + "</option>");
		$("#shopListPanel, #yinying1").hide();
	}


	window.__shopSelected = __shopSelected;
	window.openShopListPanel = openShopListPanel;
})(jQuery);
var _pagination = null;

$(function() {

	$('.doubledate').kuiDate({
		className : 'doubledate',
		isDisabled : "0" // isDisabled为可选参数，“0”表示今日之前不可选，“1”标志今日之前可选
	});

	initShopSelecter();
});

/**
 * 初始化店面选择器
 */
function initShopSelecter() {
	refreshInput($(".serWrapper"));
	$("#shopName").val('');
	$("#shopIsChoosed").removeAttr("checked");
	$("#shopName").attr("disabled", "disabled");

	$("#shopIsChoosed").bind("click", function() {
		var checked = $(this).attr("checked");
		if (checked) {
			$("#shopName").removeAttr("disabled");
		} else {
			$("#shopName").attr("disabled", "disabled");
			$("#shopName").empty();
		}
	});
}

function downRescouse() {
	var shopId = $("#shopName").val();
	if (shopId === null) {
		shopId = "";
	}
	var orderNo = $("#orderNo").val();
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
	var receivName = $("#receivName").val();
	var mp = $("#mp").val();
	var status = $("#status").val();

	var params = {
		orderNo : orderNo,
		startDate : startDate,
		endDate : endDate,
		receivName : receivName,
		mp : mp,
		status : status,
		shopId : shopId
	};

	var searchParams = $.param(params);
	window.location.href = '../orderMain/down?' + searchParams;
}

function search() {
	var shopId = $("#shopName").val();
	if (shopId === null) {
		shopId = "";
	}
	var orderNo = $("#orderNo").val();
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
	var receivName = $("#receivName").val();
	var mp = $("#mp").val();
	var status = $("#status").val();

	// 初始化代码
	_pagination = new Pagination({
		method : 'get', // 请求方法 post|get
		type : 'json', // 返回内容格式，json|text //NOT SUPPORT
		url : '../order/list/', // 请求地址，注意函数会自定添加分页的两个参数
		data : {
			orderNo : orderNo,
			startDate : startDate,
			endDate : endDate,
			receiver : receivName,
			mp : mp,
			status : status,
			shopId : shopId
		},
		pageSize : 10, // 页面大小
		startIndex : 0, // 服务器提供的接口页码是从几开始的，建议从0开始
		success : printDataToPage, // state 为 0 时，回调
		pagerHandler : 'pager' // 显示分页的 div 的 id
	});
}

function printDataToPage(message) {
	var $tbody = $("#orderList");
	$tbody.empty();
	// 清空数据
	$("#sup").text("共 " + message.result.len + " 条");
	var len = message.result.data.length;
	// 返回条目数
	for (var i = 0; i < len; i++) {

		//"orderId","orderNo","createFileTime","revceive","mp","money","status","detailAddress"

		var orderId = getHtmlString(getHeadIndexValue(i, message, 'orderId'));
		var orderNo = getHtmlString(getHeadIndexValue(i, message, 'orderNo'));
		var receivName = getHtmlString(getHeadIndexValue(i, message, 'revceive'));
		var makeTime = getHtmlString(getHeadIndexValue(i, message, 'createFileTime'));
		var mp = getHtmlString(getHeadIndexValue(i, message, 'mp'));
		var integral = getHtmlString(getHeadIndexValue(i, message, 'integral'));
		var staName = getHtmlString(getHeadIndexValue(i, message, 'status'));
		staName = getStatusName(staName);
		var tr = "<tr>" + "<td>" + orderNo + "</td>" + "<td>" + makeTime + "</td>" + "<td>" + receivName + "</td>" + "<td>" + mp + "</td>" + "<td>" + integral + "</td>" + "<td>" + staName + "</td>" + "<td>" + "<a href='#' onClick=\"javascript: toOrderDetail(" + orderId + ")\">详情</td>" + "</tr>";
		$tbody.append($(tr));
	}
}

/**
 * 显示订单详情页面
 *
 * @param orderId
 */
function toOrderDetail(orderId) {
	loadOrderDetail(orderId);
	$("#orderDetailPage").show();
	$("#orderListPage").hide();
}

/**
 * 加载订单详情
 */
function loadOrderDetail(orderId) {
	$.get('../order/detail/' + orderId, function(message) {
		if (IsEmpty(message)) {
			art.dialog({
				fixed : true,
				lock : true,
				content : '没有查询到数据！',
				icon : 'error',
				ok : true
			});
			return;
		}
		if (message.state === 0) {
			printOrderDetailToPage(message);
		} else if (message.state == 1) {
			art.dialog({
				fixed : true,
				lock : true,
				content : message.msg,
				icon : 'error',
				ok : true
			});
		} else {
			checkStateShowErrorMsgAndDealResult(JSON.stringify(message));
		}
	}, 'json');
}

/**
 * 将订单详情显示到页面
 *
 * @param message
 */
function printOrderDetailToPage(message) {

	var $tbody = $("#goodsList");
	$tbody.empty();
	var len = message.result.data.length;
	$("#goodsSup").text("共 " + len + " 条");
	var totalNum = 0;
	for (var i = 0; i < len; i++) {
		//"orderId","orderNo","status","createFileTime","revceive",
		//"mp","detailAddress","imageUrl","goodsName","mode",
		//"price","num","subTotal","money"
		var supplierName = getHtmlString(getHeadIndexValue(i, message, "SupplierName"));
		var goodsCode = getHtmlString(getHeadIndexValue(i, message, "goodsCode"));
		var goodsName = getHtmlString(getHeadIndexValue(i, message, "goodsName"));
		var mode = getHtmlString(getHeadIndexValue(i, message, "mode"));
		var num = getHtmlString(getHeadIndexValue(i, message, "num"));
		var unitPrice = getHtmlString(getHeadIndexValue(i, message, "price"));
		var unitIntegral = getHtmlString(getHeadIndexValue(i, message, "saleIntegral"));
		var subtotal = getHtmlString(getHeadIndexValue(i, message, "subTotal"));
		var subTtalIntegral = getHtmlString(getHeadIndexValue(i, message, "subTtalIntegral"));
		totalNum += num;
		var tr = "<tr><td>" + supplierName + "</td><td>" + goodsCode + "</td><td>" + goodsName + "</td><td>" + mode + "</td><td>" + num + "</td><td>" + unitIntegral + "</td><td>" + subTtalIntegral + "</td></tr>";
		$tbody.append($(tr));
	}
	var money = getHtmlString(getHeadIndexValue(0, message, "money"));
	var integral = getHtmlString(getHeadIndexValue(0, message, "integral"));
	var tr = "<tr><td colspan='4'>总计</td><td>" + totalNum + "</td><td></td><td>" + integral + "</td></tr>";
	$tbody.append($(tr));
	//"orderId","orderNo","status","createFileTime","revceive",
	//"mp","detailAddress","imageUrl","goodsName","mode",
	//"price","num","subTotal","money"
	var orderMainNo = getHtmlString(getHeadIndexValue(0, message, "orderNo"));
	var status = getHtmlString(getHeadIndexValue(0, message, "status"));
	status = getStatusName(status);
	var orderStatus = getHtmlString(getStatusName(status));
	var makeTime = getHtmlString(getHeadIndexValue(0, message, "createFileTime"));
	var receivName = getHtmlString(getHeadIndexValue(0, message, "receiver"));
	var mp = getHtmlString(getHeadIndexValue(0, message, "mp"));
	var receiveAddress = getHtmlString(getHeadIndexValue(0, message, "detailAddress"));

	$("#orderMainNo").text(orderMainNo);
	$("#receiveMp").text(mp);
	$("#receiver").text(receivName);
	$("#orderStatus").text(status);
	$("#makeTime").text(makeTime);
	$("#receiveAddress").text(receiveAddress);
	//$("#totalMoney").text(integral);
}

/**
 * 关闭订单详情页面
 */
function toOrderList() {
	_pagination.reload();
	// 重新加载订单列表页面
	$("#orderDetailPage").hide();
	$("#orderListPage").show();
}

function getStatusName(status) {
	switch (status) {
		case 0:
			return "未付款";
		case 1:
			return "已支付";
	}
}
